/* 
 * Copyright (C) 2015 fede
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package Entity;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;

/**
 *
 * @author fede
 */
@Entity(name = "REGISTEREDUSER")
@NamedQueries({
    @NamedQuery(name = RegisteredUser.FIND_USER_BY_NAME_SURNAME_AND_EMAIL,
            query = "SELECT u FROM REGISTEREDUSER u WHERE (u.name LIKE :string OR u.surname LIKE :string OR u.email LIKE :string) AND u.email<> :user"),})
public class RegisteredUser implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private String email;

    //no username
    private String password;
    private String name;
    private String surname;
    @Temporal(value = javax.persistence.TemporalType.DATE)
    private Date birthdate;
    private String address;
    private String groupName;

    /**
     * find user by name and surname
     */
    public static final String FIND_USER_BY_NAME_SURNAME_AND_EMAIL = "RegisteredUser.findUserByNameSurnameAndEmail";

    /**
     * this method get the Grouname of RegisteredUser
     *
     * @return Groupname
     */
    public String getGroupName() {
        return groupName;
    }

    /**
     * this method set the Grouname of RegisteredUser
     *
     * @param groupName to set
     */
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    /**
     * this method get the Email of RegisteredUser
     *
     * @return Email as String
     */
    public String getEmail() {
        return email;
    }

    /**
     * this method get the Email of RegisteredUser
     *
     * @param email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * this method get the Password of RegisteredUser
     *
     * @return password as string
     */
    public String getPassword() {
        return password;
    }

    /**
     * this method set the Password of RegisteredUser using "SHA-256" and
     * "UTF-8"
     *
     * @param password as String
     */
    public void setPassword(String password) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(password.getBytes("UTF-8"));
            BigInteger bigInt = new BigInteger(1, hash);
            this.password = bigInt.toString(16);
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
            Logger.getLogger(RegisteredUser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * this method get the Name of RegisteredUser
     *
     * @return name as string
     */
    public String getName() {
        return name;
    }

    /**
     * this method set the Password of RegisteredUser
     *
     * @param name as string
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * this method get the surname of RegisteredUser
     *
     * @return surname as string
     */
    public String getSurname() {
        return surname;
    }

    /**
     * this method set the Password of RegisteredUser
     *
     * @param surname as string
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * this method get the date of birth of RegisteredUser
     *
     * @return Date
     */
    public Date getBirthdate() {
        return birthdate;
    }

    /**
     * this method set the date of birth of RegisteredUser
     *
     * @param birthdate as Date
     */
    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    /**
     * this method get the address of RegisteredUser
     *
     * @return address as String
     */
    public String getAddress() {
        return address;
    }

    /**
     * this method get the address of RegisteredUser
     *
     * @param address as String
     */
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + Objects.hashCode(this.email);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final RegisteredUser other = (RegisteredUser) obj;
        if (!Objects.equals(this.email, other.email)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entity.RegistereUser[ email=" + email + " ]";
    }

}
